package EA.testproblems;
import EA.*;
import RKUjava.util.*;

/*
  Griewank Function
  
  Created: 24. Feb. 2000
  @version 1.0
  @author Rene Thomsen
*/

/* Griewank's objective function with d=10 */
public class Griewank extends NumericalProblem {
    
    // Easier way to build max and min
    private double[][] lmax = new double[0][10];
    private double[][] lmin = new double[0][10];
    
    public Griewank() {
	super();
	
	double[] optimas;
	name = "Griewank function";
	objectivefunction = new NumericalFitness() {
		public double Fitness_calcFitness_inner(double[] realpos) {
		    double fit1 = 0, fit2 = 1;
		    //calculate fitness for each individual
		    for(int i=0; i<10; i++) {
			fit1= fit1 + 
			    Math.pow((realpos[i] - 100.0),2);
		    }
		    fit1 = (1.0/4000.0)*fit1;
		    for (int i=1; i<11; i++) {
			fit2 = fit2 * Math.cos(((realpos[i-1]-100.0))/Math.sqrt(i));
		    }
		    return fit1 - fit2 + 1;
		};
	    };
	
	dimensions = 10;
	ismaximization = false;
	optimumradius = 0.2;

	intervals = new Interval[dimensions];
	for (int i=0; i<dimensions; i++)
	    intervals[i] = new Interval(-600.0, 600.0);


	// Set up known maxima
	knownmaxima = new NumericalOptimum[lmax.length];
	for (int i=0;i<lmax.length;i++) {
	    optimas = new double[dimensions];
	    for (int j=0;j<dimensions;j++) {
		optimas[j] = lmax[i][j];
	    }
	    knownmaxima[i] = new NumericalOptimum(optimas, objectivefunction.calcFitness(optimas), true, false, i);
	}
	
	// Set up known minima
	knownminima = new NumericalOptimum[lmin.length];
	
	for (int i=0;i<lmin.length;i++) {
	    optimas = new double[dimensions];
	    for (int j=0;j<dimensions;j++) {
		optimas[j] = lmin[i][j];
	    }
	    knownminima[i] = new NumericalOptimum(optimas, objectivefunction.calcFitness(optimas), false, false, i);
	}
	
    }
}
